package top.lc951.myandroid.views.card;

import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;

public class OverlayTransformerPoratior implements ViewPager.PageTransformer {
    public static final String TAG = OverlayTransformerPoratior.class.getSimpleName();
    private float transOffset = 40 * 1.65f;
    private float scaleOffset = 40;
    private float mOffset = 40;
    private final int overlayCount = 3;
    ViewPager mViewPager;

    public OverlayTransformerPoratior(float minScale) {
        mMinScale = minScale;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void transformPage(View page, float position) {
        Log.i(TAG, "position =" + position);
//        page.setAlpha(0.5f);
        if (mViewPager == null) {
            mViewPager = (ViewPager) page.getParent();
        }
        int current=mViewPager.getCurrentItem();
        Log.i(TAG,"current="+current);
        int pageWidth = page.getWidth();
        int pageHeight = page.getHeight();
//
        page.setPivotX(pageWidth / 2);
        if(position<=0){
            page.setTranslationX(-position * page.getWidth());
            //缩放卡片并调整位置
            float scale = (page.getWidth() + mOffset * position) / page.getWidth();
            page.setScaleX(scale);
            page.setScaleY(scale);
            page.setTranslationY((position) * mOffset+2.5f*current*mOffset);
            page.setTranslationZ(position);
            page.setPivotY(-1.5f*pageHeight);
        }else{
            //移动X轴坐标，使得卡片在同一坐标
            page.setTranslationX(-position * page.getWidth());

            //缩放卡片并调整位置
            float scale = (page.getWidth() - mOffset * position) / page.getWidth();
            page.setScaleX(scale);
            page.setScaleY(scale);
            //移动Y轴坐标
            page.setTranslationY((position) * mOffset+2.5f*current*mOffset);


            page.setTranslationZ(-position);
            page.setPivotY(1.5f*pageHeight);

        }
    }

    private float mMinScale = 0.85f;
    public static final float DEFAULT_MIN_SCALE = 0.85f;
    private static final float DEFAULT_CENTER = 0.85f;
    private boolean finishFirstTransx = false;

    private void otherTrans(View page, float position) {
        //缩放比例
        float scale = (page.getWidth() - scaleOffset * position) / (float) (page.getWidth());
        page.setScaleX(scale);
        page.setScaleY(scale);

        if (position > overlayCount - 1 && position < overlayCount) { //当前页向右滑动时,最右面第四个及以后页面应消失
            float curPositionOffset = transOffset * (float) Math.floor(position); //向下取整
            float lastPositionOffset = transOffset * (float) Math.floor(position - 1); //上一个卡片的偏移量
            float singleOffset = 1 - Math.abs(position % (int) position);
//            float transX = (-page.getWidth() * position) + (lastPositionOffset + singleOffset * (curPositionOffset - lastPositionOffset));
            float transX = (-page.getWidth() * position);
            page.setTranslationX(transX);
        } else if (position <= overlayCount - 1) {
            if (position == overlayCount) {
                finishFirstTransx = true;
            }
            if (!finishFirstTransx){
                float transX = (-page.getWidth() * position) + (transOffset * position);
                page.setTranslationX(transX);
            }
        } else {
            page.setAlpha(0f);
        }
    }

}
